<?php

namespace Index;

use Library\View as view;
use Library\WebSocketClient as swc;
use Model\User;
use Model\Chat;
use Illuminate\Database\Capsule\Manager as DB;
use Library\Redis;

defined('slim') or exit('访问不合法');

class IndexController
{
    //容器
    public $ci;

    public function __construct($ci)
    {
        $this->ci = $ci;
    }

    /*
     * 聊天首页
     */
    public function index($request, $response, $args)
    {
//        if(is_mobile()){
//            //手机端页面
//            return view::render([],'index_wap');
//        }
        return view::render([]);
    }

    /*
     * 获取用户列表
     */
    public function userList($request, $respose, $args)
    {
        $member_id = $request->getCookieParam('member_id');
        $userList = User::where('user_id', '<>', $member_id)->get()->toArray();
        $respose->write(JSON(200, '', $userList));
    }

    public function ceshi1()
    {
        return view::render([], 'test');
    }

    public function ceshi()
    {
        return view::render();
//        DB::table('user')->where('user_id',6)->update([
//            'user_name'=>'无霜'
//        ]);
        $res = DB::table('user')->where('user_id', '6')->first();
        dd($res);

        die;
        try {
            $redis = Redis::connect();
            $redis->multi();
            $redis->set('test1', 1);
            $redis->get('test1');
            $redis->incr('test1');
            $redis->discard();
            $redis->incr('test1');
            $r = $redis->get('test1');
            dd($r);
            $res = $redis->exec();
            dd($res);
        } catch (\Exception $e) {
            var_dump('捕获到异常:' . $e->getMessage());
        }
    }

    public function test_swoole_tcp()
    {

    }

    /*
     * redis测试
     */
    public function redis($request, $response, $args)
    {
//        $redis = Redis::connect();
        $redis = new \redis();
        $redis->pconnect('127.0.0.1', 6379, 2);
        $res = $redis->publish('chat', '聚散终有时');
        var_dump($res);
        die;
//        dd($redis->getKeys('*'));
        $key = $redis->getKeys('*');
        foreach ($key as $k => $v) {
            echo '<hr>';
            echo '<br>';
            echo '键值:' . $v;
            echo '    ';
            $type = redisType($redis->type($v));
            echo '类型:' . $type;
            echo '<br>';
            echo '内容:';
            if ($type == 'string') {
                echo $redis->get($v);
            }
            if ($type == 'list') {
                print_r($redis->lrange($v, 0, -1));
            }
            if ($type == 'set') {
                print_r($redis->smembers($v));
            }
        }
    }

    /*
     * 消息队列php-resque测试
     */
    private function resque($request, $response, $args)
    {
        date_default_timezone_set('Asia/Shanghai');
        $args = [
            'method' => 'chat',
            'args' => [
                'name' => 'gxk',
                'age' => '23'
            ]
        ];

        \Resque::setBackend('127.0.0.1:6379');
        for ($i = 1; $i < 101; $i++) {
            $jobId = \Resque::enqueue('default', '\Library\ResqueJob', ['id' => $i], true);
            echo "Queued job " . $jobId . "\n\n";
        }
    }

    public function middleware($request, $response, $args)
    {
        $res = DB::table('chat')->where([
            ['chat_user_name', '=', $args['name']],
            ['chat_id', '=', 55],
        ])->toSql();
        dd($res);
//        dd($res->isEmpty());
//        dd($res->count());
        die;
        if (empty($res)) {
            echo '暂无相关数据';
        } else {
            echo 'list';
            dd($res);
        }
//        $response->write('middleware');
    }

    public function swoole()
    {
        return view::render();
    }

    public function swoole_socket_client()
    {
//        $client = new \swoole_client(SWOOLE_SOCK_TCP);
//        if (!$client->connect('172.27.0.6', 9502))
//        {
//            exit("connect failed. Error: {$client->errCode}\n");
//        }else{
//            echo '连接成功';
//        }
//        $client->send("hello world\n");
////        echo $client->recv();
//        $client->close();
//
//        echo 'over';
//        die;


        $client = new swc('172.27.0.6', '9502');
        echo "connect......";
        echo '<br>';
        if (!$client->connect()) {
            exit("connect failed. Error: {$client->errCode}\n");
        }
        echo "connected.\n";
        echo '<br>';
        echo "send msessage=hello world\n";
        echo '<br>';
        $res = $client->send('什么个情况');
        var_dump($res);
        echo '<br>';
        echo "recv message=";
        echo '<br>';
        echo $client->recv() . "\n";
        echo "closed\n";
        $client->close();
    }

    public function test()
    {
        echo "<pre>";
        $arr = [5, 3, 7, 2, 6, 8, 0, 1];
        print_r($arr);
//        print_r(InsertSort($arr));
//        die;
        $count = count($arr);
        for ($i = 1; $i < $count; $i++) {
            $temp = $arr[$i];
            $j = $i - 1;
            while ($j >= 0 && $arr[$j] > $temp) {
                $arr[$j + 1] = $arr[$j];
                $j--;
            }
            if ($i != $j + 1) {
                $arr[$j + 1] = $temp;
            }
        }
        print_r($arr);
    }


}
